# test02_hashlib.py  计算大文件的md5值的方法
import hashlib
def check_md5(fname):  # fname: 文件路径
    m = hashlib.md5()
    with open(fname, mode="rb") as fr:
        while True:
            tmp = fr.read(4096)  # 4K
            if len(tmp) == 0:  # tmp为空时，退出while循环
                break
            m.update(tmp)  # 更新md5值
    return m.hexdigest()  # 返回给函数16进制的md5值
if __name__ == '__main__':
    print(check_md5("/etc/hosts"))
    # 54fb6627dbaa37721048e4549db3224d
    # [root@localhost day08]# md5sum /etc/hosts
    # 54fb6627dbaa37721048e4549db3224d  /etc/hosts

